<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class CaseXqService extends BaseService
{
    //原创上传

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     */
    public function getIndex($cateid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbCaseXq)->where('del', 0);
        if ($cateid) {
            $db = $db->where('cate_id', $cateid);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //企业信息
            $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            $cname = empty($comOne) ? '-' : $comOne['cname'];
            $clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
            //分类信息
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['cate_id'])->first();
            $cateName = empty($cateOne) ? '-' : $cateOne['title'];
            //设计公司
            $sellOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['seller_id'])->first();
            $sell_name = empty($sellOne) ? '-' : $sellOne['cname'];
            $arys[] = [
                'id' => $v['id'],
                'com_id' => $v['cid'],
                'com_name' => $cname,
                'com_logo' => $clogo,
                'seller_name' => $sell_name,
                'title' => $v['title'],
                'cate_id' => $v['cate_id'],
                'cate_name' => $cateName,
                'yaoqiu' => $v['yaoqiu'],
                'start_day' => $v['start_day'],
                'end_day' => $v['end_day'],
                'price' => toNumber($v['price']),
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //一个
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbCaseXq)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //企业信息
        $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['cid'])->first();
        $cname = empty($comOne) ? '-' : $comOne['cname'];
        //分类信息
        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $res['cate_id'])->first();
        $cateName = empty($cateOne) ? '-' : $cateOne['title'];
        //客户公司
        $sellOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['seller_id'])->first();
        $sell_name = empty($sellOne) ? '-' : $sellOne['cname'];
        $arys = [
            'title' => $res['title'],
            'com_id' => $res['cid'],
            'com_name' => $cname,
            'cate_id' => $res['cate_id'],
            'cate_name' => $cateName,
            'seller_name' => $sell_name,
            'yaoqiu' => $res['yaoqiu'],
            'start_day' => $res['start_day'],
            'end_day' => $res['end_day'],
            'price' => toNumber($res['price']),
            'item_detail' => $res['item_detail'] ? htmlspecialchars_decode($res['item_detail']) : '',
        ];
        return $this->toback(0, '', $arys);
    }

    //添加
    public function add($pas)
    {
        $res = (array) $this->dbConn($this->dbCaseXq)->where('title', $pas['title'])->first();
        if (!empty($res)) {
            return $this->toback(-2, '存在同名项目');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbCaseXq)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    //修改
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbCaseXq)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbCaseXq)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    /**
     * 案例删除
     * @param int $id
     */
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbCaseXq)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '该分类不存在');
        }
        $up = $this->dbConn($this->dbCaseXq)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}